/*
 Copyright (C) 2008  Maurizio Monticelli

 This program is free software; you can redistribute it and/or modify it under the terms of the 
 GNU General Public License as published by the Free Software Foundation; either version 2 of the 
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
 Public License for more details.

 You should have received a copy of the GNU General Public License along with this program; if not, write 
 to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 
*/

package ch.justdoit.andriodrpncalc.model.command;

import java.util.Stack;

import ch.justdoit.andriodrpncalc.model.CalcNumber;
import ch.justdoit.andriodrpncalc.model.Model;


public abstract class Abstract1Command extends BaseCommand {

	public Abstract1Command(Model _model) {
		super(_model);
	}

	protected CalcNumber numA;

	@Override
	public boolean doit() {
		boolean result = false;
		resetErrorMessage();
		if (mModel.stack.size() >= 1) {
			numA = mModel.stack.PopNumber();
			if (numA.isNumber()) {
				doNumberOperation();
				result = true;
				
			} else {
				CalcNumber number = new CalcNumber(mModel);
				number.setToken(doExpressionOperation());
				mModel.stack.Put(number);
			}
			
		} else {
			mModel.setErrorMessage( mCommandName + " Error: Too few arguments");
		}
		return result;
	}

	@Override
	public boolean undo() {
		boolean result = true;
		mModel.stack.Pop();
		mModel.stack.Put(numA);
		return result;
	}
	
	protected abstract void doNumberOperation();
	protected Stack<String> doExpressionOperation() {
		Stack<String> result = numA.getTokens();
		//StringBuffer expression = new StringBuffer();
		if (numA.isExpression()) {
			result.add(0, mExpression);
			if (mExpressionEnd != null) {
				result.add(mExpressionEnd);
			}
			//expression.append("abs(" + numA.getExpressionString() + ")");
		} else {
		//	expression.append(numA.toString());
		}
		
//		return expression.toString();
		return result;
	}

}
